#include <stdio.h>
#include <SDL/SDL.h>
#include "IHM.h"
#include "personnage.h"
#include "gestion.h"
#include "terrain.h"
#include "interaction_T_P.h"
#include "interaction_P_P.h"
#include "mode.h"



void Init_Bouttons( _boutton* ListeBouttons){ // besoin d'autant de valeur ?
ListeBouttons[0].SGX =176;
ListeBouttons[0].SGY =176;
ListeBouttons[0].SDX =288;
ListeBouttons[0].SDY =176;
ListeBouttons[0].IGX =176;
ListeBouttons[0].IGY =205;

ListeBouttons[1].SGX =176;  
ListeBouttons[1].SGY =214;
ListeBouttons[1].SDX =288;
ListeBouttons[1].SDY =214;
ListeBouttons[1].IGX =176;
ListeBouttons[1].IGY =242;

ListeBouttons[2].SGX =176;
ListeBouttons[2].SGY =251;
ListeBouttons[2].SDX =288;
ListeBouttons[2].SDY =251;
ListeBouttons[2].IGX =176;
ListeBouttons[2].IGY =280;

ListeBouttons[3].SGX =160;
ListeBouttons[3].SGY =300;
ListeBouttons[3].SDX =200;
ListeBouttons[3].SDY =340;
ListeBouttons[3].IGX =160;
ListeBouttons[3].IGY =340;

ListeBouttons[4].SGX =210; 
ListeBouttons[4].SGY =300;
ListeBouttons[4].SDX =250;
ListeBouttons[4].SDY =300;
ListeBouttons[4].IGX =210;
ListeBouttons[4].IGY =340;

ListeBouttons[5].SGX =260; 
ListeBouttons[5].SGY =300;
ListeBouttons[5].SDX =300;
ListeBouttons[5].SDY =300;
ListeBouttons[5].IGX =260;
ListeBouttons[5].IGY =340;

ListeBouttons[6].SGX =310; 
ListeBouttons[6].SGY =300;
ListeBouttons[6].SDX =350;
ListeBouttons[6].SDY =300;
ListeBouttons[6].IGX =310;
ListeBouttons[6].IGY =340;

ListeBouttons[7].SGX =160; 
ListeBouttons[7].SGY =400;
ListeBouttons[7].SDX =200;
ListeBouttons[7].SDY =400;
ListeBouttons[7].IGX =160;
ListeBouttons[7].IGY =440;

ListeBouttons[8].SGX =210; 
ListeBouttons[8].SGY =400;
ListeBouttons[8].SDX =250;
ListeBouttons[8].SDY =400;
ListeBouttons[8].IGX =210;
ListeBouttons[8].IGY =440;

ListeBouttons[9].SGX =260; 
ListeBouttons[9].SGY =400;
ListeBouttons[9].SDX =300;
ListeBouttons[9].SDY =400;
ListeBouttons[9].IGX =260;
ListeBouttons[9].IGY =440;

ListeBouttons[10].SGX =310; 
ListeBouttons[10].SGY =400;
ListeBouttons[10].SDX =350;
ListeBouttons[10].SDY =400;
ListeBouttons[10].IGX =310;
ListeBouttons[10].IGY =440;

ListeBouttons[11].SGX =100; 
ListeBouttons[11].SGY =500;
ListeBouttons[11].SDX =300;
ListeBouttons[11].SDY =500;
ListeBouttons[11].IGX =100;
ListeBouttons[11].IGY =580;

}


int Boutton_Clic ( const _boutton* ListeBoutton, const int* Clic ) { // renvoi le boutton cliqué
int X = Clic[0];
int Y = Clic[1];
int i;

for ( i=0; i<=13; i++) {
	
	if ( (X > ListeBoutton[i].SGX) && (X < ListeBoutton[i].SDX) && (Y > ListeBoutton[i].SGY) && (Y < ListeBoutton[i].IGY) )
		return i;

}
// il manque un else ... comment le placer ?

}


void pause() {
    int continuer = 1;
    SDL_Event event;
 
    while (continuer)
    {
        SDL_WaitEvent(&event);
        switch(event.type)
        {
            case SDL_KEYDOWN:
                continuer = 0;
        }
    }
}






void Afficher_Menu (_configuration* Config){

    _boutton ListeBouttons[12];
    Init_Bouttons(ListeBouttons);

    SDL_Event Event;
    SDL_Surface *ecran = NULL, *ImageDeFond = NULL;
    SDL_Rect positionFond;
   
    int Clic[2];    // X puis Y
    int Boutton;
    int continuer = 1;	
    positionFond.x = 0;
    positionFond.y = 0;



    ecran = SDL_SetVideoMode(660, 660, 32, SDL_HWSURFACE);
    SDL_WM_SetCaption("Role & Stratégie", NULL);

    ImageDeFond = SDL_LoadBMP("./Image/ecran_titre.bmp");
    SDL_BlitSurface(ImageDeFond, NULL, ecran, &positionFond);

  
    SDL_Flip(ecran);
    
Set_NomCarte(Config,0);

while (continuer){

SDL_WaitEvent(&Event);


if ( Event.type == SDL_MOUSEBUTTONDOWN) {

            Clic[0] = (Event.button.x);
            Clic[1] = (Event.button.y);
            
    

Boutton = Boutton_Clic(ListeBouttons, Clic );
 switch(Boutton) {
	case 0:
	Set_NomCarte(Config,1);
	printf(" Nom carte  %d", Config->NomCarte);	
	break;
	case 1:
	Set_NomCarte(Config,2);
	printf(" Nom carte  %d", Config->NomCarte);
	break;
	case 2:
	Set_NomCarte(Config,3);
	printf(" Nom carte  %d", Config->NomCarte);
	break;
	case 3:
	Set_NbJoueurs(Config,1);
	printf(" NB joueur  %d", Config->NbJoueurs);
	break;
	case 4:
	Set_NbJoueurs(Config,2);
	printf(" NB joueur  %d", Config->NbJoueurs);
	break;
	case 5:
	Set_NbJoueurs(Config,3);
	printf(" NB joueur  %d", Config->NbJoueurs);
	break;
	case 6:
	Set_NbJoueurs(Config,4);
	printf(" NB joueur  %d", Config->NbJoueurs);
	break;
	case 7:
	Set_NbIa(Config,0);
	printf(" NB Ia  %d", Config->NbIa);
	break;
	case 8:
	Set_NbIa(Config,1);
	printf(" NB Ia  %d", Config->NbIa);
	break;
	case 9:
	Set_NbIa(Config,2);
	printf(" NB Ia  %d", Config->NbIa);
	break;
	case 10:
	Set_NbIa(Config,3);
	printf(" NB Ia  %d", Config->NbIa);
	break;
	case 11:
	if ( (Get_NbJoueurs(Config)+Get_NbIa(Config)) <=4 && Get_NomCarte(Config) != 0  )	
		continuer =0;
	break;
	

}

printf (" continuer = %d \n",continuer);
printf (" boutton = %d \n",Boutton);

}// if

}// Fin du while continuer


    SDL_FreeSurface(ImageDeFond);
 
}     


void Jeu (_terrain* Terrain, _joueur** Joueur, _personnage** ListePerso, _configuration* Config){
    SDL_Event Event;
    SDL_Surface *ecran = NULL, *ImageDeFond = NULL,*placement1 = NULL,*placement2 = NULL,*placement3 = NULL,*placement4 = NULL;
    SDL_Rect positionFond,positionPlacement1,positionPlacement2,positionPlacement3,positionPlacement4;
   
    int Clic[2];    // X puis Y
    int continuer = 1;	
    positionFond.x = 0;
    positionFond.y = 0;

    positionPlacement1.x = TAILLECASE*7;
    positionPlacement1.y = TAILLECASE*0;

    positionPlacement2.x = TAILLECASE*18;
    positionPlacement2.y = TAILLECASE*7;

    positionPlacement3.x = TAILLECASE*7;
    positionPlacement3.y = TAILLECASE*18;

    positionPlacement4.x = TAILLECASE*0;
    positionPlacement4.y = TAILLECASE*7;

    ecran = SDL_SetVideoMode(990, 990, 32, SDL_HWSURFACE);
    SDL_WM_SetCaption("Role & Stratégie", NULL);

placement1 = SDL_CreateRGBSurface(SDL_HWSURFACE, 8*TAILLECASE, 4*TAILLECASE, 32, 0, 0, 0, 0);
placement2 = SDL_CreateRGBSurface(SDL_HWSURFACE, 4*TAILLECASE, 8*TAILLECASE, 32, 0, 0, 0, 0);
placement3 = SDL_CreateRGBSurface(SDL_HWSURFACE, 8*TAILLECASE, 4*TAILLECASE, 32, 0, 0, 0, 0);
placement4 = SDL_CreateRGBSurface(SDL_HWSURFACE, 4*TAILLECASE, 8*TAILLECASE, 32, 0, 0, 0, 0);


SDL_FillRect(placement1, NULL, SDL_MapRGB(placement1->format, 56, 45, 255));
SDL_SetAlpha(placement1, SDL_SRCALPHA, 75);
SDL_FillRect(placement2, NULL, SDL_MapRGB(placement2->format, 255, 31, 15));
SDL_SetAlpha(placement2, SDL_SRCALPHA, 75);
SDL_FillRect(placement3, NULL, SDL_MapRGB(placement3->format, 13, 255, 5));
SDL_SetAlpha(placement3, SDL_SRCALPHA, 75);
SDL_FillRect(placement4, NULL, SDL_MapRGB(placement4->format, 255, 204, 0));
SDL_SetAlpha(placement4, SDL_SRCALPHA, 75);


 switch( Get_Nom(Terrain) ) {

case 1 :
	ImageDeFond = SDL_LoadBMP("./Image/prairie.bmp");
	
	break;
case 2 : 
	ImageDeFond = SDL_LoadBMP("./Image/foret.bmp");
	break;
case 3 :
	ImageDeFond = SDL_LoadBMP("./Image/cuvette.bmp");
	break;
}


SDL_BlitSurface(ImageDeFond, NULL, ecran, &positionFond);
SDL_BlitSurface(placement1, NULL, ecran, &positionPlacement1);
SDL_BlitSurface(placement2, NULL, ecran, &positionPlacement2);
SDL_BlitSurface(placement3, NULL, ecran, &positionPlacement3);
SDL_BlitSurface(placement4, NULL, ecran, &positionPlacement4);







  
    SDL_Flip(ecran);

pause();
SDL_FreeSurface(ImageDeFond);
SDL_FreeSurface(placement1);
SDL_FreeSurface(placement2);
SDL_FreeSurface(placement3);
SDL_FreeSurface(placement4);
}




void Ecran_Aide(){


    SDL_Surface *ecran = NULL, *ImageDeFond = NULL;
    SDL_Rect positionFond;

    positionFond.x = 0;
    positionFond.y = 0;

    SDL_Init(SDL_INIT_VIDEO);
    ecran = SDL_SetVideoMode(800, 600, 32, SDL_HWSURFACE);
    SDL_WM_SetCaption("Chargement d'Images en SDL", NULL);

    ImageDeFond = SDL_LoadBMP("./Image/instruction.bmp");
    SDL_BlitSurface(ImageDeFond, NULL, ecran, &positionFond);

    SDL_Flip(ecran);
    pause();

    SDL_FreeSurface(ImageDeFond);



}


SDL_Surface*** Chargement_Affichage_Personnage(){

SDL_Surface*** Sprites;
int i;

Sprites = (SDL_Surface***) malloc (sizeof(SDL_Surface**)*12);


for (i = 0; i<12; i++){
Sprites[i] = (SDL_Surface**) malloc (sizeof(SDL_Surface*)*4);
}

Sprites[0][0] =SDL_LoadBMP("./Image/araignee_face.bmp");
Sprites[0][1] =SDL_LoadBMP("./Image/araignee_droite.bmp");
Sprites[0][2] =SDL_LoadBMP("./Image/araignee_gauche.bmp");
Sprites[0][3] =SDL_LoadBMP("./Image/araignee_dos.bmp");
Sprites[1][0] =SDL_LoadBMP("./Image/arbre_face.bmp");
Sprites[1][1] =SDL_LoadBMP("./Image/arbre_droite.bmp");
Sprites[1][2] =SDL_LoadBMP("./Image/arbre_gauche.bmp");
Sprites[1][3] =SDL_LoadBMP("./Image/arbre_dos.bmp");
Sprites[2][0] =SDL_LoadBMP("./Image/loup_face.bmp");
Sprites[2][1] =SDL_LoadBMP("./Image/loup_droite.bmp");
Sprites[2][2] =SDL_LoadBMP("./Image/loup_gauche.bmp");
Sprites[2][3] =SDL_LoadBMP("./Image/loup_dos.bmp");
Sprites[3][0] =SDL_LoadBMP("./Image/brute_face.bmp");
Sprites[3][1] =SDL_LoadBMP("./Image/brute_droite.bmp");
Sprites[3][2] =SDL_LoadBMP("./Image/brute_gauche.bmp");
Sprites[3][3] =SDL_LoadBMP("./Image/brute_dos.bmp");
Sprites[4][0] =SDL_LoadBMP("./Image/dragon_face.bmp");
Sprites[4][1] =SDL_LoadBMP("./Image/dragon_droite.bmp");
Sprites[4][2] =SDL_LoadBMP("./Image/dragon_gauche.bmp");
Sprites[4][3] =SDL_LoadBMP("./Image/dragon_dos.bmp");
Sprites[5][0] =SDL_LoadBMP("./Image/squelette_face.bmp");
Sprites[5][1] =SDL_LoadBMP("./Image/squelette_droite.bmp");
Sprites[5][2] =SDL_LoadBMP("./Image/squelette_gauche.bmp");
Sprites[5][3] =SDL_LoadBMP("./Image/squelette_dos.bmp");
Sprites[6][0] =SDL_LoadBMP("./Image/guerrier_faec.bmp");
Sprites[6][1] =SDL_LoadBMP("./Image/guerrier_droite.bmp");
Sprites[6][2] =SDL_LoadBMP("./Image/guerrier_gauche.bmp");
Sprites[6][3] =SDL_LoadBMP("./Image/guerrier_dos.bmp");
Sprites[7][0] =SDL_LoadBMP("./Image/mage_face.bmp");
Sprites[7][1] =SDL_LoadBMP("./Image/mage_droite.bmp");
Sprites[7][2] =SDL_LoadBMP("./Image/mage_gauche.bmp");
Sprites[7][3] =SDL_LoadBMP("./Image/mage_dos.bmp");
Sprites[8][0] =SDL_LoadBMP("./Image/soldat_face.bmp");
Sprites[8][1] =SDL_LoadBMP("./Image/soldat_droite.bmp");
Sprites[8][2] =SDL_LoadBMP("./Image/soldat_gauche.bmp");
Sprites[8][3] =SDL_LoadBMP("./Image/soldat_dos.bmp");
Sprites[9][0] =SDL_LoadBMP("./Image/knuckle_face.bmp");
Sprites[9][1] =SDL_LoadBMP("./Image/knuckle_droite.bmp");
Sprites[9][2] =SDL_LoadBMP("./Image/knuckle_gauche.bmp");
Sprites[9][3] =SDL_LoadBMP("./Image/knuckle_dos.bmp");
Sprites[10][0] =SDL_LoadBMP("./Image/tails_face.bmp");
Sprites[10][1] =SDL_LoadBMP("./Image/tails_droite.bmp");
Sprites[10][2] =SDL_LoadBMP("./Image/tails_gauche.bmp");
Sprites[10][3] =SDL_LoadBMP("./Image/tails_dos.bmp");
Sprites[11][0] =SDL_LoadBMP("./Image/sonic_face.bmp");
Sprites[11][1] =SDL_LoadBMP("./Image/sonic_droite.bmp");
Sprites[11][2] =SDL_LoadBMP("./Image/sonic_gauche.bmp");
Sprites[11][3] =SDL_LoadBMP("./Image/sonic_dos.bmp");

return Sprites;
}


void Afficher_Perso(Sprites,ecran,xy,i) {


NULL;


}
